package com.dewod.producesales.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.dewod.producesales.R;
import com.dewod.producesales.model.view.SettingItem;

import java.util.List;

public class SettingCenterAdapter extends RecyclerView.Adapter<SettingCenterAdapter.ViewHolder>{

    private static final String TAG = "SettingCenter";

    private List<SettingItem> data;

    private OnItemClickListener itemClickListener; // 点击事件监听器


    public SettingCenterAdapter(List<SettingItem> data) {
        this.data = data;
    }

    public SettingCenterAdapter(List<SettingItem> data, OnItemClickListener itemClickListener) {
        this.data = data;
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 在这里加载 CardView 布局并创建 ViewHolder
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_settings, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 在这里设置每个卡片的内容
        SettingItem item = data.get(position);
        holder.icon.setImageResource(item.getIconResId());
        holder.title.setText(item.getTitle());
        if(ObjectUtils.isNotEmpty(item.getContent())){
            holder.content.setText(item.getContent());
        }else{
            holder.content.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当ViewHolder中的视图被点击时，调用点击事件监听器的方法
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(item);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView icon;

        TextView title;

        TextView content;


        public ViewHolder(View itemView) {
            super(itemView);

            icon = itemView.findViewById(R.id.icon);
            if(icon == null){
                Log.e(TAG, "icon is null in ViewHolder");
            }


            title = itemView.findViewById(R.id.title);
            if (title == null) {
                Log.e(TAG, "title is null in ViewHolder");
            }

            content = itemView.findViewById(R.id.content);
            if (content == null) {
                Log.e(TAG, "content is null in ViewHolder");
            }

        }
    }

    public interface OnItemClickListener {
        void onItemClick(SettingItem item);
    }
}
